//
//  ModelParseUtil.m
//  NewsReader
//
//  Created by blacksun on 10-10-26.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ModelParseUtil.h"


@implementation ModelParseUtil

+ (NSMutableArray *) parseSectionsFromJsonArray:(NSArray *)jsonArray{
	NSMutableArray *sections = [NSMutableArray array];
	for (NSDictionary *dict in jsonArray) {
		Section *temp = [[Section alloc] init];
		temp.sectionId = [dict objectForKey:@"id"];
		temp.sectionName = [dict objectForKey:@"name"];
		temp.lastFeedTitle = [dict objectForKey:@"lastFeedTitle"];
		temp.lastFeedImageUrl = [dict objectForKey:@"lastFeedImageURL"];
		temp.imageUrl = [dict objectForKey:@"image"];
		temp.summary = [dict objectForKey:@"summary"];
		temp.refreshDate =  [dict objectForKey:@"refreshDate"];
		temp.oldImageUrl = @"noimage";
		id leaf = [dict objectForKey:@"leaf"];
		temp.childSections = [ModelParseUtil parseSectionsFromJsonArray:[dict objectForKey:@"sections"]];
		temp.leaf = [leaf boolValue];
		temp.currentMaxId = [((NSString *)[dict objectForKey:@"lastFeedIndex"]) integerValue];
		temp.currentMinId =  0;
		[sections addObject:temp];
		[temp release];
	}
	return sections;
} 


+ (NSMutableArray *) parseFeedsFromFromResponse:(NSData *)response{
	NSMutableArray *formatedFeeds = [[[NSMutableArray alloc] init] autorelease];
	if (!response) {
		return formatedFeeds;
	}
	NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *json = [str JSONValue];
	NSArray *feeds = [json objectForKey:@"feeds"];
	for (NSDictionary *feed in feeds) {
		Feed *temp = [[Feed alloc] init];
		temp.feedId = [feed objectForKey:@"id"];
		temp.sectionId = [feed objectForKey:@"sectionId"];
        temp.subjectId = [feed objectForKey:@"subjectId"];
        temp.sectionIndex = [((NSString *)[feed objectForKey:@"sectionIndex"]) intValue];
        temp.subjectIndex = [((NSString *)[feed objectForKey:@"subjectIndex"]) intValue];
		temp.description = [feed objectForKey:@"summary"];
		temp.author = [feed objectForKey:@"source"];
		temp.title = [feed objectForKey:@"title"];
		temp.link = [feed objectForKey:@"link"];
		temp.pubDate = [feed objectForKey:@"pubDate"];
		temp.complete = [feed objectForKey:@"complete"];
		NSArray *newDetails = [feed objectForKey:@"detail"];
		NSMutableArray *details = [[NSMutableArray alloc] init];
		for (NSDictionary *detail in newDetails) {
			Content *content = [[Content alloc] init];
			content.feedId = temp.feedId;
			content.type = [detail objectForKey:@"type"];
			content.width = [(NSString *)[detail objectForKey:@"width"] integerValue];
			content.heigth = [(NSString *)[detail objectForKey:@"height"] integerValue];
			content.content = [detail objectForKey:@"content"];
			[details addObject:content];
			[content release];
		}
		temp.details = details;
		[details release];
		
		temp.thumb = [feed objectForKey:@"thumb"];
		if (!temp.thumb) {
			temp.thumb = @"";
		}
		temp.createDate = [feed objectForKey:@"pubDate"];
		[formatedFeeds addObject:temp];
		[temp release];
	}
	
	//NSLog(@"解析结束");
	[str release];
	
	return formatedFeeds;
	
}



//重新构建scrollview
+ (NSInteger) reloadScrollView:(UIScrollView *)scrollView withControllers:(NSArray *)controllers 
				 needAnimation:(BOOL)needAnimation startIndex:(NSInteger)index{
	for (UIView *view in scrollView.subviews) {
		[view removeFromSuperview];
	}
	NSInteger pageCount = [controllers count]/6;
	if ([controllers count]%6 != 0) {
		pageCount ++;
	}
	
	scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*pageCount, scrollView.frame.size.height);
	
	NSInteger prevPage = 0;
	for (int i=0; i<pageCount; i++) {
		UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width*i, 0.0, scrollView.frame.size.width, scrollView.frame.size.height)];
		for (int j=0; j<6; j++) {
			NSInteger currentIndex = i*6+j;
			if (currentIndex < [controllers count]) {
				SectionViewController *sectionController = [controllers objectAtIndex:currentIndex];
				
				if (currentIndex!=0 && currentIndex%5==0 && currentIndex+1<=[controllers count]) {
					SectionViewController *next = [controllers objectAtIndex:currentIndex];
					next.view.frame = CGRectMake(325.0, 5.0, 155.0, 120.0);
				}
				prevPage = i;
				if (needAnimation && index<=currentIndex) {
					[UIView beginAnimations:nil context:nil];
					[UIView setAnimationDuration:0.5];
					[UIView setAnimationDelay:0.15*j];
					index ++;
				}
				if (currentIndex == [controllers count]-1) {
					sectionController.view.frame = CGRectMake((j%2)*155.0+5,(j/2+j%1)*120.0-4.0+10,157.0,122.0);
				}else {
					sectionController.view.frame = CGRectMake((j%2)*155.0,(j/2+j%1)*120.0,155.0,120.0);
				}

				[view addSubview:sectionController.view];
				
				if (needAnimation && index>=currentIndex) {
					[UIView commitAnimations];
				}
				
				
			}
		}
		[scrollView addSubview:view];
		[view release];
	}
	return pageCount;
}
+(void) addAnimationForSections:(NSArray *)sectionControllers{
	NSInteger pageCount = [sectionControllers count]/6;
	if ([sectionControllers count]%6 != 0) {
		pageCount ++;
	}
	for (int i=0; i<pageCount; i++) {
		for (int j=0; j<6; j++) {
			NSInteger currentIndex = i*6+j;
			if (currentIndex < [sectionControllers count]) {
				SectionViewController *sectionController = [sectionControllers objectAtIndex:currentIndex];
				[UIView beginAnimations:nil context:nil];
				[UIView setAnimationDuration:2.0];
				[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
				sectionController.view.alpha = 1.0;
				[UIView commitAnimations];
				
			}
		}
	}
	
}

@end
